<template>
  <!-- <van-button
    round
    size="small"
    :loading="loading"
    v-if="isFollowed"
    @click="onFollow"
    >已关注</van-button
  >
  <van-button
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="loading"
    v-else
    @click="onFollow"
    >关注</van-button
  > -->
  <van-button
    round
    size="small"
    :loading="loading"
    v-if="value"
    @click="onFollow"
    >已关注</van-button
  >
  <van-button
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="loading"
    v-else
    @click="onFollow"
    >关注</van-button
  >
</template>

<script>
// 加载请求关注方法
import { deleteFollow, addFollow } from '@/api/user'
export default {
  name: 'Followindex',
  components: {},
  props: {
    // isFollowed: {
    //   type: Boolean,
    //   required: true
    // },
    value: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Object, String, Number],
      required: true
    }
  },
  data() {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onFollow() {
      this.loading = true
      try {
        if (this.value) {
          // 已关注状态，取消关注
          await deleteFollow(this.userId)
        } else {
          // 未关注状态，添加关注
          await addFollow(this.userId)
        }
        // 更新视图状态 传值
        // this.$emit('update-is_followed', !this.isFollowed)
        this.$emit('input', !this.value)
      } catch (error) {
        let message = '请求失败，稍后重试！'
        if (error.response && error.response.status === 400) {
          message = '不能关注自己'
        }
        this.$toast(message)
      }
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped></style>
